<template>

    <div>
        <p>
            <button @click="list" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh green"></i>
                刷新
            </button>
            &nbsp;
            <button @click="add" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit red"></i>
                新增
            </button>
        </p>
        <pagination ref="pagination" v-bind:list="list"></pagination>

        <div class="space-4"></div>


        <div class="row">
            <div v-for="teacher in teachers" class="col-md-2">
                <div>
          <span class="profile-picture">
            <img v-show="!teacher.image" class="editable img-responsive editable-click editable-empty" src="/static/image/讲师头像/头像1.jpg" v-bind:title="teacher.intro"/>
            <img v-show="teacher.image" class="editable img-responsive editable-click editable-empty" v-bind:src="teacher.image" v-bind:title="teacher.intro"/>
          </span>

                    <div class="space-4"></div>

                    <div class="width-85 label label-info label-xlg arrowed-in arrowed-in-right">
                        <div class="inline position-relative">
                            <p href="#" class="user-title-label dropdown-toggle">
                                <i class="ace-icon fa fa-circle light-green"></i>
                                &nbsp;
                                <span class="white">{{teacher.position}}</span>
                            </p>
                        </div>
                    </div>
                </div>

                <div class="space-6"></div>

                <div class="text-center">
                    <a href="javascript:;" class="text-info bigger-110" v-bind:title="teacher.motto">
                        <i class="ace-icon fa fa-user"></i>
                        {{teacher.name}}【{{teacher.nickname}}】
                    </a>
                </div>

                <div class="space-6"></div>

                <div class="profile-social-links align-center">
                    <button v-on:click="edit(teacher)" class="btn btn-xs btn-info">
                        <i class="ace-icon fa fa-pencil bigger-120"></i>
                    </button>
                    &nbsp;
                    <button v-on:click="handledelete(teacher.id)" class="btn btn-xs btn-danger">
                        <i class="ace-icon fa fa-trash-o bigger-120"></i>
                    </button>
                </div>

                <div class="hr hr16 dotted"></div>

            </div>
        </div>

        <!-- /.span -->

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">

                            <div class="form-group">
                                <label class="col-sm-2 control-label">姓名:</label>
                                <div class="col-sm-10">
                                    <input type="text" v-model="teacherinfo.name" class="form-control" placeholder="姓名">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称:</label>
                                <div class="col-sm-10">
                                    <input type="text" v-model="teacherinfo.nickname" class="form-control"
                                           placeholder="昵称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">头像:</label>
                                <div class="col-sm-10">
                                    <oss-file v-bind:text="'上传头像'" v-bind:input-id="'image-upload'"
                                          v-bind:after-upload="afterUpload"
                                          v-bind:suffixs="['jpg','jpeg','png']"
                                          v-bind:use="FILE_USE.TEACHER.key"></oss-file>
                                    <div v-show="teacherinfo.image" class="row">
                                        <div class="col-md-4">
                                            <img v-bind:src="teacherinfo.image" class="img-responsive"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">职位:</label>
                                <div class="col-sm-10">
                                    <input type="text" v-model="teacherinfo.position" class="form-control"
                                           placeholder="职位">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">座右铭:</label>
                                <div class="col-sm-10">
                                    <input type="text" v-model="teacherinfo.motto" class="form-control"
                                           placeholder="座右铭">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">简介:</label>
                                <div class="col-sm-10">
                                    <textarea type="text" v-model="teacherinfo.intro" class="form-control" rows="5"
                                              placeholder="简介"/>
                                </div>
                            </div>

                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" @click="save" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->


    </div><!-- /.row -->


</template>


<script scope>
    import axios from 'axios';
    import {defineComponent, onMounted, ref} from 'vue';
    import Pagination from '@/components/pagination.vue';
    import BigFile from "../../components/big-file.vue";
    import OssFile from "../../components/oss-file.vue";
    import File from "../../components/file.vue";

    export default defineComponent({
        name: 'teacher',
        components:{
            BigFile,File,OssFile,
            Pagination
        },
        setup() {
            const teachers = ref();
            const pagination = ref({});
            const teacherinfo=ref({});
            let file=ref();
            // let FILE_USE=FILE_USE;
            const list = () => {
                Loading.show();
                axios.get("/business/admin/teacher/list", {
                    params: {
                        page: pagination.value.page,
                        size: pagination.value.size,
                    }
                }).then((response) => {
                    Loading.hide();
                    const data=response.data;
                    if (data.success){
                        teachers.value = data.content.list;
                        pagination.value.render(pagination.value.page,data.content.total);
                    }else {
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            const add=()=>{
                teacherinfo.value={};
                $('#myModal').modal();
            };

            /**
             * 编辑
             */
            const edit = (teacher) => {
                $('#myModal').modal();
                teacherinfo.value = Tool.copy(teacher);
            };

            /**
             * 删除
             */
            const handledelete = (id) => {
                Confirm.show("删除讲师后不可恢复，确认删除？", function () {
                    Loading.show();
                    axios.delete("/business/admin/teacher/delete/" + id).then((response) => {
                        Loading.hide();
                        const data = response.data;
                        if (data.success) {
                            list();
                            Toast.success("删除成功!");
                        }else{
                            Loading.hide();
                            Toast.warning(data.message);
                        }
                    })
                });
            };

            const save=()=>{
                // 保存校验
                if (1 != 1
                    || !Validator.require(teacherinfo.value.name, "姓名")
                    || !Validator.length(teacherinfo.value.name, "姓名", 1, 50)
                    || !Validator.length(teacherinfo.value.nickname, "昵称", 1, 50)
                    || !Validator.length(teacherinfo.value.image, "头像", 1, 100)
                    || !Validator.length(teacherinfo.value.position, "职位", 1, 50)
                    || !Validator.length(teacherinfo.value.motto, "座右铭", 1, 50)
                    || !Validator.length(teacherinfo.value.intro, "简介", 1, 500)
                ) {
                    return;
                }

                Loading.show();
                axios.post("/business/admin/teacher/save",teacherinfo.value).then((response) => {
                    const data=response.data;
                    if (data.success){
                        Loading.hide();
                        $('#myModal').modal('hide');
                        // console.log(data);
                        list();
                        Toast.success("保存成功！");
                    }else{
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            const afterUpload=(data)=>{
                let image=data.content.path;
                 teacherinfo.value.image=image;
            }


            onMounted(() => {
                list();
            });

            return {
                list,
                add,
                edit,
                handledelete,
                pagination,
                teacherinfo,
                save,
                teachers,
                file,
                afterUpload,
                FILE_USE
            }


        }
    });
</script>